package com.ruoyi.hr.service.impl;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.domain.Departments;
import com.ruoyi.hr.mapper.AuthorityMapper;
import com.ruoyi.hr.service.AuthorityService;
import com.ruoyi.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AuthorityServiceImpl implements AuthorityService {

    @Autowired
    AuthorityMapper authorityMapper;

    @Override
    public List<Authority> getAuthority() {
        return authorityMapper.getAuthority();
    }

    @Override
    public List<String> getDepAuthority(List<Authority> authorities) {
        return null;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public String getCompName(Integer compId) {
        return authorityMapper.getCompName(compId);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public String getDepName(Authority authority) {
        return authorityMapper.getDepName(authority);
    }

    @Override
    public List<Authority> getAuthorityUser(String userID) {
        return authorityMapper.getAuthorityUser(userID);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public String getDepId(Authority authority) {
        return authorityMapper.getDepId(authority);
    }

    @Override
    public int insertAuthority(Authority authority) {
        return authorityMapper.insertAuthority(authority);
    }

    @Override
    public int deleteAuthority(String ids) {
        return authorityMapper.deleteAuthority(ids);
    }

    @Override
    public Authority getAuthorityId(String id) {
        return authorityMapper.getAuthorityId(id);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Departments> getDepList(String depIds) {
        return authorityMapper.getDepList(depIds);
    }

    @Override
    public int updateAuthority(Authority authority) {
        return authorityMapper.updateAuthority(authority);
    }

    @Override
    public List<SysUser> getUserList() {
        return authorityMapper.getUserList();
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public String getSubordinateCompId(int compId) {
        return authorityMapper.getSubordinateCompId(compId);
    }

    @Override
    public int setUpAdmin(Authority authority) {
        return authorityMapper.setUpAdmin(authority);
    }

    @Override
    public int findAuthorityCount(String userId) {
        return authorityMapper.findAuthorityCount(userId);
    }

    @Override
    public Authority findAuthorAndIsAdmin() {
        SysUser user = ShiroUtils.getSysUser();
        List<Authority> authorityList = authorityMapper.getAuthorityUser(user.getUserId().toString());
        int isAdmin = 0;
        if (authorityList.size() == 1) {
            if (authorityList.get(0).getIsAdmin() != null) {
                isAdmin = 1;
            }
        }
        Authority authority = new Authority(authorityList, isAdmin + "");
        return authority;
    }


}
